/*
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *    2010 year
 *    Author: Michael Khromov aka MikeDM (Mike D'Morto)
 *    E-Mail: mikedmorto@gmail.com
 */
#include "dcfg.h"
#include "ui_dcfg.h"
#include <QDebug>
#include <QStringList>
#include <QMessageBox>

dcfg::dcfg(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::dcfg)
{
    ui->setupUi(this);
    connect(this->ui->btn_add,SIGNAL(clicked()),SLOT(slotAdd()));
    connect(this->ui->tw_re,SIGNAL(cellDoubleClicked(int,int)),SLOT(slotEditCell(int,int)));
    connect(this->ui->btn_edit,SIGNAL(clicked()),SLOT(slotEdit()));
    connect(this->ui->btn_del,SIGNAL(clicked()),SLOT(slotDel()));
    connect(this->ui->btn_up,SIGNAL(clicked()),SLOT(slotUp()));
    connect(this->ui->btn_down,SIGNAL(clicked()),SLOT(slotDown()));

}

dcfg::~dcfg()
{
    delete ui;
}

void dcfg::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void dcfg::slotAdd(){
    DRule * ruledia = new DRule();
    if(ruledia->exec()){
        QStringList tt=ruledia->getRule();

        // добавляем новое правило в список.
        this->ui->tw_re->setRowCount(this->ui->tw_re->rowCount()+1);
        for(int i = 0; i <= 2 ; i++){
            QTableWidgetItem *newItem = new QTableWidgetItem(tt.at(i));
            this->ui->tw_re->setItem(this->ui->tw_re->rowCount()-1,i,newItem);
        }
    }
    delete ruledia;

}

void dcfg::slotEditCell(int a, int b){
    this->slotEdit();
}

void dcfg::slotEdit(){
    int row=this->ui->tw_re->currentRow();
    if(row>=0){
        DRule * ruledia = new DRule(this->ui->tw_re->item(row,0)->text(),
                                    this->ui->tw_re->item(row,1)->text(),
                                    this->ui->tw_re->item(row,2)->text());
        if(ruledia->exec()){
            // есть изменения, применим их.
            QStringList tt=ruledia->getRule();
            for(int i = 0 ; i <=2 ; i++){
                this->ui->tw_re->item(row,i)->setText(tt.at(i));
            }
        }
        delete ruledia;

    }
}

void dcfg::slotDel(){
    if(this->ui->tw_re->currentRow() >=0){
        if(QMessageBox::warning(this,"TITLE","Delete rule <"+
                            this->ui->tw_re->item(this->ui->tw_re->currentRow(),0)->text()+"> ?",
                            QMessageBox::Ok, QMessageBox::Cancel
                            ) == QMessageBox::Ok){
            qDebug()<<"DELETE YES";
            this->ui->tw_re->removeRow(this->ui->tw_re->currentRow());
        }
    }
}

void dcfg::slotUp(){
    if(this->ui->tw_re->currentRow()>=1){
        for(int i= 0 ; i <=2 ; i++){
            QTableWidgetItem * it0 = this->ui->tw_re->takeItem(this->ui->tw_re->currentRow(),i);
            QTableWidgetItem * it1 = this->ui->tw_re->takeItem(this->ui->tw_re->currentRow()-1,i);
            this->ui->tw_re->setItem(this->ui->tw_re->currentRow()-1,i,it0);
            this->ui->tw_re->setItem(this->ui->tw_re->currentRow(),i,it1);
        }
    }
}

void dcfg::slotDown(){
    if(this->ui->tw_re->currentRow() >=0 and this->ui->tw_re->rowCount() > this->ui->tw_re->currentRow()+1){
        for(int i= 0 ; i <=2 ; i++){
            QTableWidgetItem * it0 = this->ui->tw_re->takeItem(this->ui->tw_re->currentRow(),i);
            QTableWidgetItem * it1 = this->ui->tw_re->takeItem(this->ui->tw_re->currentRow()+1,i);
            this->ui->tw_re->setItem(this->ui->tw_re->currentRow()+1,i,it0);
            this->ui->tw_re->setItem(this->ui->tw_re->currentRow(),i,it1);
        }
    }
}


void dcfg::setRulesCount(int count){
    this->ui->tw_re->setRowCount(count);
}
